﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class zc : WeiPage
{
    PersonInfoBO pbo = new PersonInfoBO();
    IntegralChangeBO ibo = new IntegralChangeBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=d9bfd969e149c1844698cfd1f962bdad&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "" + GetAppid() + "";
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = pbo.GetModel(user);
                if (per != null)
                {
                    Label1.Text = "你已注册";
                    Button1.Visible = false;
                    Button2.Visible = false;
                    Response.Redirect("Index.aspx");
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {

            if (txtPhone.Value.Trim().Length != 11)
            {
                Label1.Text = "手机号码为11位数字";
            }
            else if (txtNum.Value.Trim() == "")
            {
                Label1.Text = "请输入验证码";
            }
            else
            {
                si_DispMember_obService s1 = new si_DispMember_obService();
                dt_DispMember_req w1 = new dt_DispMember_req();
                w1.ZTYPE = "0003";
                w1.MOB_NUMBER = txtPhone.Value.Trim();
                dt_DispMember_res dt1 = s1.si_DispMember_ob(w1);
                if (dt1.ZCRMT316 == null)
                {
                    Label1.Text = "客户不存在，请进行注册";
                }
                else
                {
                    string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                    string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                    PersonInfo per = pbo.GetModel(user);
                    if (per != null)
                    {
                        Label1.Text = "你已经验证成功";
                        Button2.Visible = false;
                        Button1.Visible = false;

                    }
                    else
                    {
                        per = pbo.GetModel("", txtPhone.Value);
                        //if (per == null)
                        //{
                        //    Label1.Text = "验证失败，请联系管理员";
                        //}
                        //else
                        //{
                            if (txtNum.Value == "")
                            {
                                Label1.Text = "请获取验证码";
                            }
                            else if (Session["RanNum"]!=null&&Session["RanNum"].ToString()!= txtNum.Value.Trim())
                            {
                                Label1.Text = "验证码不正确。";
                            }
                            else
                            {
                                if (per != null)
                                {
                                    per.FromUserName = user;
                                    per.WX_DATE = DateTime.Now;
                                    int num = pbo.Update(per);
                                    if (num > 0)
                                    {
                                        Label1.Text = "验证成功";
                                        try
                                        {
                                            si_UploadMemberShip_obService s = new si_UploadMemberShip_obService();
                                            dt_UploadMemberShip_req w = new dt_UploadMemberShip_req();
                                            ZCRMT316 z = new ZCRMT316();
                                            z.TYPE = "0003";
                                            z.BPEXT = per.PersonNo;
                                            z.MOB_NUMBER = per.Phone;
                                            z.ZC023 = user;
                                            w.ZCRMT316 = new ZCRMT316[] { z };
                                            dt_UploadMemberShip_res dt = s.si_UploadMemberShip_ob(w);
                                        }
                                        catch (Exception)
                                        {

                                        }
                                        Button2.Visible = false;
                                        Button1.Visible = false;
                                        Response.Redirect("Index.aspx");
                                    }
                                }
                                else
                                {
                                    try
                                    {


                                        per = new PersonInfo();
                                        per.addr = dt1.ZCRMT316[0].PSTREET == null ? "" : dt1.ZCRMT316[0].PSTREET;
                                        per.area = dt1.ZCRMT316[0].REGION == null ? "" : dt1.ZCRMT316[0].REGION;
                                        per.BIRTHDAY = dt1.ZCRMT316[0].BIRTHDT == null ? DateTime.Parse("1900-01-01") : dt1.ZCRMT316[0].BIRTHDT;
                                        per.CreateDate = dt1.ZCRMT316[0].REG_DATE == null ? DateTime.Parse("1900-01-01") : dt1.ZCRMT316[0].REG_DATE;
                                        per.EMAIL = dt1.ZCRMT316[0].CSMTP_ADDR == null ? "" : dt1.ZCRMT316[0].CSMTP_ADDR;
                                        per.FromUserName = user;
                                        per.LAST_DATE = DateTime.Now;
                                        per.MEM_DATE = DateTime.Now;
                                        per.MEM_LEVEL = dt1.ZCRMT316[0].ZTIER == null ? "" : getlv(dt1.ZCRMT316[0].ZTIER);
                                        per.Name = dt1.ZCRMT316[0].NAME1_TEXT == null ? "" : dt1.ZCRMT316[0].NAME1_TEXT;
                                        per.PersonNo = dt1.ZCRMT316[0].BPEXT == null ? "" : dt1.ZCRMT316[0].BPEXT;
                                        per.Phone = txtPhone.Value;
                                        per.REMARK = "";
                                        per.Sex = dt1.ZCRMT316[0].XSEX == "1" ? true : false;
                                        per.Store = dt1.ZCRMT316[0].DEPTID == null ? "" : dt1.ZCRMT316[0].DEPTID;
                                        per.SumIntegral = int.Parse(dt1.ZCRMT316[0].ZCCUR_POINT.ToString());
                                        per.SurplusIntegral = int.Parse(dt1.ZCRMT316[0].ZCCUR_POINT.ToString());
                                        per.WX_DATE = DateTime.Now;
                                        per.YZM = txtNum.Value;
                                        int num = pbo.Add(per);
                                        if (num > 0)
                                        {
                                            Label1.Text = "验证成功";
                                            try
                                            {
                                                si_UploadMemberShip_obService s = new si_UploadMemberShip_obService();
                                                dt_UploadMemberShip_req w = new dt_UploadMemberShip_req();
                                                ZCRMT316 z = new ZCRMT316();
                                                z.TYPE = "0003";
                                                z.BPEXT = per.PersonNo;
                                                z.MOB_NUMBER = per.Phone;
                                                z.ZC023 = user;
                                                w.ZCRMT316 = new ZCRMT316[] { z };
                                                dt_UploadMemberShip_res dt = s.si_UploadMemberShip_ob(w);
                                            }
                                            catch (Exception)
                                            {

                                            }
                                            Button2.Visible = false;
                                            Button1.Visible = false;
                                            Response.Redirect("Index.aspx");

                                        }
                                    }
                                    catch (Exception)
                                    {

                                        
                                    }
                                }
                            }
                        }
                    }
                //}
            }
        }
    }

    public string getlv(string lv)
    {
        if (lv == "ZSMCJ0102")
            return "珍珠卡会员";
        if (lv == "ZSMCJ0103")
            return "翡翠卡会员";
        if (lv == "ZSMCJ0104")
            return "钻石卡会员";
        else 
            return "注册会员";
    }


    /// <summary>
    /// 在客户端弹出对话框
    /// </summary>
    public void AlertMessage(string message)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertCloseReload", "<script>alert(\"" + message + "\");</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        // numT.Value = "60";
        try
        {
            Label1.Text = "";
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                si_DispMember_obService s1 = new si_DispMember_obService();
                dt_DispMember_req w1 = new dt_DispMember_req();
                w1.ZTYPE = "0003";
                w1.MOB_NUMBER = txtPhone.Value.Trim();
                dt_DispMember_res dt1 = s1.si_DispMember_ob(w1);
                if (dt1.ZCRMT316 == null)
                {
                    Label1.Text = "客户不存在，请进行注册";
                }
                else
                {
                    PersonInfo per = pbo.GetModel("", txtPhone.Value);
                    
                        if (txtPhone.Value.Length == 11)
                        {
                            Random r = new Random();
                            int num = r.Next(100000, 999999);
                            string message = string.Format("本次微信平台获取的验证码是：" + num);
                            string msg = new SendDX().Send(message, txtPhone.Value, 0);
                            if (msg == "发送成功")
                            {
                                Label1.Text = "发送成功";
                                Session["RanNum"] = num.ToString();
                                //Button2.Visible = false;
                                numT.Value = "60";
                                AlertMessage("因相关管控，经特殊处理，电信、联通客户将由136-1175-7392发送注册或绑定验证码，该号码仅提供验证码发送服务，请勿直接回复任何信息。");
                            }
                            else
                            {
                                Session["RanNum"] = num.ToString();
                                Label1.Text = "发送失败";
                            }
                            if (per != null)
                            {
                            per.YZM = num.ToString();
                            pbo.Update(per);
                            }
                            //else
                            //{
                               
                                   
                            //}
                        }
                        else
                        {
                            Label1.Text = "请输入正确的手机号";
                        }
                   
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message.ToString();
        }
    }
}